package com.coffee.admin.controller.supplier;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.common.query.GtRegionQuery;
import com.coffee.admin.api.common.result.GtRegionResult;
import com.coffee.admin.api.common.service.GtRegionService;
import com.coffee.admin.api.platform.req.PlatformSystemDeployReq;
import com.coffee.admin.api.platform.req.PlatformSystemDeployUpdateReq;
import com.coffee.admin.api.supplier.query.SupplierSystemDeployQuery;
import com.coffee.admin.api.supplier.req.SupplierSystemDeployUpdateReq;
import com.coffee.admin.api.supplier.result.SupplierSystemDeployResult;
import com.coffee.admin.api.supplier.service.SupplierSystemDeployService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.BeanUtils;
import com.coffee.sku.api.inquire.query.InquireSkuPriceTrendQuery;
import com.coffee.sku.api.inquire.result.InquireSkuPriceTrendResult;
import com.coffee.sku.api.inquire.service.InquireSkuPriceTrendService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier/inquire")
public class SupplierSystemController extends BaseController {

    @DubboReference
    private SupplierSystemDeployService supplierSystemDeployService;
    @DubboReference
    private InquireSkuPriceTrendService inquireSkuPriceTrendService;
    @DubboReference
    private GtRegionService gtRegionService;

    @OperLog(operModul = "供应商-系统管理", operDesc = "查询系统设置列表")
    @SupplierLoginCheck(alias = "SupplierSystemManageListAlias")
    @PostMapping(value = "/system/list")
    public Result<Page<SupplierSystemDeployResult>> platformSystemDeployList(@RequestBody PlatformSystemDeployReq req, HttpServletRequest request, HttpServletResponse response) {
        LoginUser supplier = getSupplier();
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        SupplierSystemDeployQuery query = new SupplierSystemDeployQuery();
        query.setSupplierId(supplier.getSupplierId());
        Page<SupplierSystemDeployResult> pager = supplierSystemDeployService.getPager(query, pagerInfo);
        return Result.success(pager);
    }


    @OperLog(operModul = "供应商-系统管理", operDesc = "修改系统设置")
    @SupplierLoginCheck(alias = "SupplierSystemOperationAlias")
    @PostMapping(value = "/system/update")
    public Result<String> updateSystem(@RequestBody SupplierSystemDeployUpdateReq req, HttpServletRequest request, HttpServletResponse response) {
        LoginUser supplier = getSupplier();
        SupplierSystemDeployResult result = new SupplierSystemDeployResult();
        BeanUtils.copyProperties(result, req);
        result.setSupplierId(supplier.getSupplierId());
        try {
            supplierSystemDeployService.addOrUpdate(result);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return Result.success("SUCCESS");
    }


    @OperLog(operModul = "供应商-价格趋势新增", operDesc = "价格趋势新增")
    @SupplierLoginCheck(alias = "SupplierSkuPriceTrendOperationAlias")
    @PostMapping(value = "/sku/priceTrend/add")
    public Result<String> skuPriceTrendAdd(@RequestBody InquireSkuPriceTrendResult result, HttpServletRequest request, HttpServletResponse response) {
        LoginUser supplier = getSupplier();
        result.setSupplierId(supplier.getSupplierId());
        try {
            inquireSkuPriceTrendService.addOrUpdate(result);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return Result.success("SUCCESS");
    }

    @OperLog(operModul = "供应商-价格趋势删除", operDesc = "价格趋势删除")
    @SupplierLoginCheck(alias = "SupplierSkuPriceTrendOperationAlias")
    @PostMapping(value = "/sku/priceTrend/delete")
    public Result<String> skuPriceTrendDelete(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) {
        LoginUser supplier = getSupplier();
        Integer id = json.getInteger("id");
        inquireSkuPriceTrendService.delete(id, supplier.getSupplierId());
        return Result.success("SUCCESS");
    }



    @SupplierLoginCheck(alias = "SupplierGtRegionListListAlias")
    @PostMapping(value = "/gtRegion/list")
    public Result<Page<GtRegionResult>> skuPriceTrendMap(@RequestBody GtRegionQuery param, HttpServletResponse response) {
        PagerInfo pagerInfo = setDefaultPageInfo(param.getPageSize(), param.getPageIndex());
        param.setIsMustCity("Y");
        param.setType(2);
        Page<GtRegionResult> pager = gtRegionService.getPager(param, pagerInfo);
        return Result.success(pager);
    }

    @OperLog(operModul = "供应商-价格趋势图", operDesc = "价格趋势图")
    @SupplierLoginCheck(alias = "SupplierSkuPriceTrendListListAlias")
    @PostMapping(value = "/sku/priceTrend/map")
    public Result<Map<String, List<InquireSkuPriceTrendResult>>> skuPriceTrendMap(@RequestBody JSONObject jsonObject, HttpServletResponse response) {
        LoginUser supplier = getSupplier();
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        Map<String, List<InquireSkuPriceTrendResult>> map = inquireSkuPriceTrendService.getMap(supplier.getSupplierId(), startTime, endTime);
        return Result.success(map);
    }

    @OperLog(operModul = "供应商-价格趋势图", operDesc = "价格趋势图")
    @SupplierLoginCheck(alias = "SupplierSkuPriceTrendListListAlias")
    @PostMapping(value = "/sku/priceTrend/list")
    public Result<Page<InquireSkuPriceTrendResult>> skuPriceTrendList(@RequestBody JSONObject jsonObject, HttpServletResponse response) {
        LoginUser supplier = getSupplier();
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        InquireSkuPriceTrendQuery param = new InquireSkuPriceTrendQuery();
        param.setSupplierId(supplier.getSupplierId());
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<InquireSkuPriceTrendResult> pager = inquireSkuPriceTrendService.getPager(param, pagerInfo);
        return Result.success(pager);
    }


}
